using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using GTA;
using GTA.Native;
using KiwiCheckBoxes;

namespace KiwiUITest
{
    public class Example : Script
    {
        // KiwiUI class usage
        public static KiwiUI Kiwi;
        public static KiwiCheckBox box1;
        public static KiwiCheckBox box2;

        public Example()
        {
            Kiwi = new KiwiUI();

            // CheckBoxes Items
            box1 = new KiwiCheckBox();
            box2 = new KiwiCheckBox();

            Ped Player = Game.Player.Character;

            KeyDown += OnKeyDown;

            // CheckBoxes Event Handler
            box1.CheckBoxStateChanged += OnBoxChanged;
            box2.CheckBoxStateChanged += OnBoxChanged;

            // Button Creations and menus
            Kiwi.CreateMenuItem("Cars", () => ShowSubMenu("Cars"));
            Kiwi.CreateMenuItem("Weapons", () => ShowSubMenu("Weapons"));

            // Style modifications
            Kiwi.title = "Kiwi Menu";
            Kiwi.titleTextColor = Color.FromArgb(255, 50, 205, 50); // LimeGreen
            Kiwi.itemTextColor = Color.Aqua;
            Kiwi.SelectedItemColor = Color.FromArgb(100, 135, 206, 250); // LightSkyBlue (slightly more blue)
            Kiwi.titleFont = GTA.Font.Pricedown;
            Kiwi.itemsFont = GTA.Font.HouseScript;

            // Key Event
            void OnKeyDown(object sender, KeyEventArgs e)
            {
                // Toggle visibility of the Kiwi menu on key press
                if (e.KeyCode == Keys.K)
                {
                    Kiwi.Visible = !Kiwi.Visible;
                }
            }

            void ShowNotification(string message)
            {
                // Display a notification in the game
                UI.Notify(message);
            }

            void ShowSubMenu(string submenuName)
            {
                // Clear existing menu items before creating new ones
                Kiwi.ClearMenuItems();

                // Dictionary to add menus and items
                Dictionary<string, Action> submenu = GetSubMenu(submenuName, box1); // Put any checkbox item in the 2nd slot of the GetSubMenu method
                foreach (var menuItem in submenu)
                {
                    Kiwi.CreateMenuItem(menuItem.Key, menuItem.Value);
                }

                // Important to keep, so you can go back to the main menu
                Kiwi.CreateMenuItem("Back", () => ShowMainMenu());
            }

            // Main Menu
            void ShowMainMenu()
            {
                // Clear existing menu items
                Kiwi.ClearMenuItems();

                // Reset menu title
                Kiwi.title = "Kiwi Menu";

                // Reset current menu item index
                Kiwi.currentMenuItem = 0;

                // Recreate main menu items
                Kiwi.CreateMenuItem("Cars", () => ShowSubMenu("Cars"));
                Kiwi.CreateMenuItem("Weapons", () => ShowSubMenu("Weapons"));
            }

            Dictionary<string, Action> GetSubMenu(string submenuName, KiwiCheckBox box)
            {
                // Dictionary to store submenu items
                Dictionary<string, Action> submenu = new Dictionary<string, Action>();

                switch (submenuName)
                {
                    case "Cars":
                        // Set submenu title
                        Kiwi.title = "Cars";

                        // Add checkbox and vehicle spawn items (Example, can be changed)
                        submenu.Add(box1.CreateCheckBox("Toggle CheckBox1"), () => CheckBox1());
                        submenu.Add("Spawn Adder", () => SpawnVehicle(VehicleHash.Adder));
                        submenu.Add("Spawn Chimera", () => SpawnVehicle(VehicleHash.Chimera));
                        submenu.Add("Spawn Tractor", () => SpawnVehicle(VehicleHash.Tractor));
                        submenu.Add("Spawn Stalion2", () => SpawnVehicle(VehicleHash.Stalion2));
                        break;
                    case "Weapons":
                        // Set submenu title
                        Kiwi.title = "Weapons";

                        // Add checkbox and weapon-related items (Example, can be changed)
                        submenu.Add(box2.CreateCheckBox("Toggle CheckBox2"), () => CheckBox2());
                        submenu.Add("Give All Weapons", () => GiveAllWeapons());
                        break;
                    // Add more submenus as needed
                    default:
                        break;
                }

                return submenu;
            }

            void GiveAllWeapons()
            {
                // Give the player all weapons
                foreach (var weaponName in Enum.GetNames(typeof(WeaponHash)))
                {
                    // Skip non-weapon entries
                    if (Enum.TryParse(weaponName, out WeaponHash weaponHash))
                    {
                        Function.Call(Hash.GIVE_WEAPON_TO_PED, Game.Player.Character.Handle, (int)weaponHash, 999, false, true);
                    }
                }

                ShowNotification("All weapons given to the player!");
            }

            void CheckBox1()
            {
                // Toggle invincibility checkbox state
                box1.IsChecked = !box1.IsChecked;
            }

            void CheckBox2()
            {
                // Toggle "hello" checkbox state
                box2.IsChecked = !box2.IsChecked;
            }

            void OnBoxChanged(object sender, EventArgs e)
            {
                // Handle checkbox changes based on the current submenu
                // The current menu title is used to determine which menu the item has been placed in
                if (Kiwi.title == "Cars")
                {
                    ShowSubMenu("Cars");
                }
                else if (Kiwi.title == "Weapons")
                {
                    ShowSubMenu("Weapons");
                }
            }

            void SpawnVehicle(VehicleHash vehicle)
            {
                // Find the nearest vehicle to the player
                Vehicle nearestVehicle = World.GetClosestVehicle(Player.Position, 2.0f);

                // Delete the nearest vehicle if it exists
                if (nearestVehicle != null && nearestVehicle.Exists())
                {
                    nearestVehicle.Delete();
                }

                // Spawn the new vehicle
                Vehicle newVehicle = World.CreateVehicle(vehicle, Player.Position);
                newVehicle.PlaceOnGround();
                newVehicle.Heading = Player.Heading;

                // Warp the player into the new vehicle
                Player.Task.WarpIntoVehicle(newVehicle, VehicleSeat.Driver);

                ShowNotification($"Spawned: {vehicle}");
            }
        }
    }
}
